//
//  collectViewController.m
//  tangDynasty
//
//  Created by 洪闽程 on 2018/11/19.
//  Copyright © 2018年 洪闽程. All rights reserved.
//

#import "collectViewController.h"
#import "collectViewCell.h"
#import "MDHttpService.h"
#import "dataModel.h"
#import <MJExtension.h>
#import "MDObjManager.h"
#import "CLPlayerView.h"
#import "videoViewController.h"
#import "MBProgressHUD+MD.h"
@interface collectViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (strong, nonatomic) UITableView *tableView;
/**<##>*/
@property (nonatomic,strong)NSArray<dataModel *> * dataArr;

@property (weak, nonatomic) IBOutlet UIButton *cleanButton;
@property (nonatomic,weak) CLPlayerView *playerView;
/**<##>*/
@property (nonatomic,strong)NSMutableArray<dataModel *> * rowArr;
@property (weak, nonatomic) IBOutlet UIView *bgView;

/**<##>*/
@property (nonatomic,strong)NSArray * arr;
@end

@implementation collectViewController

-(void)viewWillAppear:(BOOL)animated
{
    
    [self.navigationController setNavigationBarHidden:YES];
    
    self.rowArr=[NSKeyedUnarchiver unarchiveObjectWithFile:[MDObjManager KeyedArchiverPathName:@"data.text"]];
    
    self.bgView.hidden=YES;
    self.cleanButton.hidden=NO;
    
    [self.tableView reloadData];
}



- (void)viewDidLoad
{
    [super viewDidLoad];
    

    
    [self.view addSubview:self.tableView];
    
    [self.tableView reloadData];
}

-(NSMutableArray<dataModel *> *)rowArr
{
    if (!_rowArr)
    {
        
        
        if (![NSKeyedUnarchiver unarchiveObjectWithFile:[MDObjManager KeyedArchiverPathName:@"data.text"]])
        {
            _rowArr=[NSMutableArray array];
        }
        
    }
    return _rowArr;
}


-(UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-120) style:UITableViewStylePlain];
        _tableView.contentInset=UIEdgeInsetsMake(0, 0,0, 0);
        _tableView.delegate=self;
        _tableView.dataSource=self;
        
        [_tableView registerNib:[UINib nibWithNibName:@"collectViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
        
        _tableView.tableHeaderView=[[UIView alloc]init];
        
        _tableView.tableHeaderView.backgroundColor=[UIColor redColor];
        
    }
    return _tableView;
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.dataArr=[NSKeyedUnarchiver unarchiveObjectWithFile:[MDObjManager KeyedArchiverPathName:@"homeData.text"]];
    
    for (int i=0; i<self.dataArr.count; i++)
    {
        if ([self.rowArr[indexPath.section].name isEqualToString:self.dataArr[i].name])
        {
            videoViewController *videoVc=[[videoViewController alloc]init];
            videoVc.selectRow=i;
            videoVc.model=self.dataArr[i];
            
            [self presentViewController:videoVc animated:YES completion:nil];
        }
    }
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    collectViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    cell.model=self.rowArr[indexPath.section];
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  self.rowArr.count;
}
- (IBAction)cleanAllClick:(id)sender
{
    
    
    [NSKeyedArchiver archiveRootObject:[NSMutableArray array] toFile:[MDObjManager KeyedArchiverPathName:@"data.text"]];
    
    self.rowArr=[NSKeyedUnarchiver unarchiveObjectWithFile:[MDObjManager KeyedArchiverPathName:@"data.text"]];
    self.bgView.hidden=YES;
    self.cleanButton.hidden=NO;
    [self.tableView reloadData];
    
}

- (IBAction)finshClick:(UIButton *)sender
{
    self.cleanButton.hidden=NO;
    self.bgView.hidden=YES;
}

- (IBAction)cleanClick:(UIButton *)sender
{
    self.bgView.hidden=NO;
    sender.hidden=YES;
}
@end
